package com.erp.app.login.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import com.erp.framework.controller.form.SimpleUserStyleForm;

public class LoginForm extends SimpleUserStyleForm {
    private String companyCode;
    private String userID;
    private String password;
    private String password2;
    private String create;
    private String sysLang;

    public String getCompanyCode() {
        return companyCode;
    }

    public String getUserID() {
        return userID;
    }

    public String getPassword() {
        return password;
    }

    public String getPassword2() {
        return password2;
    }

    public void setCompanyCode(String companyCode) {
        this.companyCode = companyCode;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPassword2() {
        this.password2 = password2;
    }

    public void otherReset(ActionMapping mapping, HttpServletRequest request) {
        String emptyString = "";
        userID = emptyString;
        password = emptyString;
        password2 = emptyString;
        create = emptyString;
    }

    public ActionErrors otherValidate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();

        if (userID == null || userID.length() == 0)
            errors.add("userID", new ActionError("error.login.user_id.blank"));

        setCodeUpperCase();

        return errors;
    }

    public ActionErrors validateNewPassword(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();

        if (password2.length() < 6) {
            errors.add(errors.GLOBAL_ERROR, new ActionError("error.login.password.too_short"));
        }
        if (password2.length() > 10) {
            errors.add(errors.GLOBAL_ERROR, new ActionError("error.login.password.too_long"));
        }
        for (int i = 0; i < password2.length(); i++) {
            char c = password2.charAt(i);
            if (!Character.isLetterOrDigit(c) || Character.isWhitespace(c)) {
                errors.add(errors.GLOBAL_ERROR, new ActionError("error.login.password.invalid"));
                break;
            }
        }
        return errors;
    }

    public void setCodeUpperCase() {
        userID = (userID == null) ? "" : userID.toUpperCase();
    }

    public String getSysLang() {
        return sysLang;
    }

    public void setSysLang(String sysLang) {
        this.sysLang = sysLang;
    }
}
